package com.bwie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbDictMapper;
import com.bwie.mapper.TbProdAttaMapper;
import com.bwie.mapper.TbProdInfoMapper;
import com.bwie.pojo.TbDict;
import com.bwie.pojo.TbProd;
import com.bwie.pojo.TbProdAtta;
import com.bwie.pojo.TbProdInfo;
import com.bwie.service.TbProdService;
import com.bwie.mapper.TbProdMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.Date;
import java.util.List;

/**
* @author 31803
* @description 针对表【tb_prod(商品表)】的数据库操作Service实现
* @createDate 2025-07-19 08:36:29
*/
@Service
public class TbProdServiceImpl extends ServiceImpl<TbProdMapper, TbProd>
    implements TbProdService{

    @Autowired
    TbProdMapper tbProdMapper;

    @Autowired
    TbDictMapper tbDictMapper;

    @Autowired
    TbProdAttaMapper tbProdAttaMapper;

    @Autowired
    TbProdInfoMapper tbProdInfoMapper;

    @Override
    public R getprodListPage(PageInfoVo pageInfoVo) {

        //构造分页参数
        Page<TbProd> page = new Page<>(pageInfoVo.getPageNum(),pageInfoVo.getPageSize());
        //查询分页数据

        Page<TbProd> tbProdPage = tbProdMapper.selecProdtPage(page, pageInfoVo);

        return R.OK(tbProdPage);
    }

    @Override
    public R getDeleteBatch(List<TbProd> prodList) {

        if(prodList.size()==0||prodList==null){
            return R.ERROR(500,"请选择数据");
        }
        for (TbProd tbProd : prodList) {

            tbProdMapper.deleteById(tbProd.getProdId());
        }

        return R.OK();
    }

    @Override
    public R getDictList(Integer typeId) {
        List<TbDict> tbDictList = tbDictMapper.selectList(
                new QueryWrapper<TbDict>().lambda().eq(TbDict::getDictType, typeId)
        );


        return R.OK(tbDictList);
    }

    @Override
    public R getAddProd(TbProd tbProd) {
        //添加审计字段
        tbProd.setCreateTime(new Date());
        tbProd.setCreateBy("管理员");

        //唯一校验
        TbProd tbProd1 = tbProdMapper.selectOne(new QueryWrapper<TbProd>().lambda().eq(TbProd::getProdSpu, tbProd.getProdSpu()));
        if(tbProd1!=null){
            return R.ERROR(500,"商品SUP重复");
        }
        //添加商品
        tbProdMapper.insert(tbProd);

        return R.OK();
    }

    @Override
    public R getAddProdAtta(TbProdAtta tbProdAtta) {

        //添加审计字典
        tbProdAtta.setCreateTime(new Date());
        tbProdAtta.setCreateBy("管理员");
//        if(tbProdAtta.getDownFlag()==1){
//
//        }

        //添加商品附件
        tbProdAttaMapper.insert(tbProdAtta);

        return R.OK();
    }

    @Override
    public R getProdInfoList(PageInfoVo pageInfoVo) {


        //构造分页参数
        Page<TbProdInfo> page = new Page<>(pageInfoVo.getPageNum(),pageInfoVo.getPageSize());
        //查询分页数据

        QueryWrapper<TbProdInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(TbProdInfo::getInfoId);
        Page<TbProdInfo> tbProdInfoPage = tbProdInfoMapper.selectPage(page, wrapper);
        List<TbProdInfo> records = tbProdInfoPage.getRecords();
        for (TbProdInfo record : records) {
            record.setBuyNum(1);
        }

        return R.OK(tbProdInfoPage);
    }

    @Override
    public R getProdandInfoListById(Integer prodId) {

        //获取商品
        TbProd tbProd = tbProdMapper.selectById(prodId);
        if(tbProd==null){
            return R.ERROR(500,"商品不存在");
        }

        //存入商品明细表
        List<TbProdInfo> tbProdInfos = tbProdInfoMapper.selectList(
                new QueryWrapper<TbProdInfo>()
                        .lambda().eq(TbProdInfo::getProdId, prodId)
        );
        tbProd.setTbProdInfos(tbProdInfos);

        return R.OK(tbProd);
    }

    @Override
    public R getAddProdInfo(TbProdInfo tbProdInfo) {
        //商品明细添加
        tbProdInfo.setCreateBy("管理员");
        tbProdInfo.setCreateTime(new Date());

        tbProdInfoMapper.insert(tbProdInfo);


        return R.OK();
    }
}




